<template>
	<view class="container">
		<!-- 搜索栏 -->
		<view class="search-box">
			<view class="search-input" @tap="goSearch">
				<text class="iconfont icon-search"></text>
				<text class="placeholder">搜索二次元商品</text>
			</view>
			<view class="search-right">
				<text class="iconfont icon-cart" @tap="goCart"></text>
				<text class="iconfont icon-user" @tap="goUser"></text>
			</view>
		</view>
		
		<!-- 轮播图 -->
		<swiper class="banner" circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, index) in bannerList" :key="index" @tap="handleBannerClick(item)">
				<image :src="item.image" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		
		<!-- 分类导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in categoryList" :key="index" @tap="goCategory(item)">
				<image :src="item.icon" mode="aspectFit"></image>
				<text>{{item.name}}</text>
			</view>
		</view>
		
		<!-- 活动专区 -->
		<view class="activity-section">
			<view class="section-title">
				<text class="title">活动专区</text>
				<text class="more" @tap="goActivity">更多</text>
			</view>
			<view class="activity-list">
				<view class="activity-item" v-for="(item, index) in activityList" :key="index" @tap="goActivityDetail(item)">
					<image :src="item.image" mode="aspectFill"></image>
					<view class="activity-info">
						<text class="name">{{item.name}}</text>
						<text class="time">{{item.time}}</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<view class="product-section">
			<view class="section-title">
				<text class="title">热门商品</text>
				<text class="more" @tap="goMore">更多</text>
			</view>
			<view class="product-list">
				<view class="product-item" v-for="(item, index) in productList" :key="index" @tap="goDetail(item)">
					<image :src="item.image" mode="aspectFill"></image>
					<view class="product-info">
						<text class="title">{{item.title}}</text>
						<text class="desc">{{item.desc}}</text>
						<view class="price-box">
							<text class="price">¥{{item.price}}</text>
							<text class="original-price">¥{{item.originalPrice}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerList: [
					{ image: '/static/banner/banner1.jpg', id: 1, type: 'product' },
					{ image: '/static/banner/banner2.jpg', id: 2, type: 'activity' },
					{ image: '/static/banner/banner3.jpg', id: 3, type: 'product' }
				],
				categoryList: [
					{ name: '一次元', icon: '/static/category/manga.png', id: 1 },
					{ name: '二次元', icon: '/static/category/figure.png', id: 2 },
					{ name: '三次元', icon: '/static/category/star.png', id: 3 },
					{ name: '二手交易', icon: '/static/category/used-figure.png', id: 4 }
				],
				activityList: [
					{
						id: 1,
						name: '2024春季新品特惠',
						time: '2024.03.01-2024.03.31',
						image: '/static/activity/activity1.jpg'
					},
					{
						id: 2,
						name: '手办模型节',
						time: '2024.03.15-2024.03.20',
						image: '/static/activity/activity2.jpg'
					}
				],
				productList: [
					{
						id: 1,
						title: '初音未来 2024手办',
						desc: '正版授权 精致做工',
						price: '299.00',
						originalPrice: '399.00',
						image: '/static/product/product1.jpg'
					},
					{
						id: 2,
						title: '原神 甘雨手办',
						desc: '限定版 带特典',
						price: '599.00',
						originalPrice: '699.00',
						image: '/static/product/product2.jpg'
					}
				]
			}
		},
		onLoad() {
			// 获取首页数据
			this.getHomeData()
		},
		methods: {
			getHomeData() {
				// TODO: 调用接口获取首页数据
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			goCart() {
				uni.navigateTo({
					url: '/pages/cart/cart'
				})
			},
			goUser() {
				uni.navigateTo({
					url: '/pages/user/user'
				})
			},
			handleBannerClick(item) {
				if (item.type === 'product') {
					this.goDetail({ id: item.id })
				} else if (item.type === 'activity') {
					this.goActivityDetail({ id: item.id })
				}
			},
			goCategory(item) {
				uni.navigateTo({
					url: `/pages/site/index?id=${item.id}`
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: `/pages/product/detail?id=${item.id}`
				})
			},
			goActivity() {
				uni.navigateTo({
					url: '/pages/activity/list'
				})
			},
			goActivityDetail(item) {
				uni.navigateTo({
					url: `/pages/activity/detail?id=${item.id}`
				})
			},
			goMore() {
				uni.navigateTo({
					url: '/pages/product/list'
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 20rpx;
		background: #f5f5f5;
	}

	.search-box {
		display: flex;
		align-items: center;
		padding: 20rpx;
		background: #fff;
		
		.search-input {
			flex: 1;
			display: flex;
			align-items: center;
			height: 60rpx;
			background: #f5f5f5;
			border-radius: 30rpx;
			padding: 0 30rpx;
			margin-right: 20rpx;
			
			.icon-search {
				font-size: 28rpx;
				color: #999;
				margin-right: 10rpx;
			}
			
			.placeholder {
				font-size: 28rpx;
				color: #999;
			}
		}
		
		.search-right {
			display: flex;
			align-items: center;
			
			.iconfont {
				font-size: 40rpx;
				color: #333;
				margin-left: 30rpx;
			}
		}
	}

	.banner {
		height: 300rpx;
		
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
		background: #fff;
		margin-bottom: 20rpx;
		
		.nav-item {
			width: 20%;
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-bottom: 20rpx;
			
			image {
				width: 80rpx;
				height: 80rpx;
				margin-bottom: 10rpx;
			}
			
			text {
				font-size: 24rpx;
				color: #333;
			}
		}
	}

	.activity-section {
		background: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
		
		.section-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}
			
			.more {
				font-size: 24rpx;
				color: #999;
			}
		}
		
		.activity-list {
			display: flex;
			flex-wrap: wrap;
			
			.activity-item {
				width: 50%;
				padding: 10rpx;
				box-sizing: border-box;
				
				image {
					width: 100%;
					height: 200rpx;
					border-radius: 8rpx;
				}
				
				.activity-info {
					padding: 10rpx;
					
					.name {
						font-size: 28rpx;
						color: #333;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						overflow: hidden;
					}
					
					.time {
						font-size: 24rpx;
						color: #999;
						margin-top: 6rpx;
					}
				}
			}
		}
	}

	.product-section {
		background: #fff;
		padding: 20rpx;
		
		.section-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}
			
			.more {
				font-size: 24rpx;
				color: #999;
			}
		}
		
		.product-list {
			display: flex;
			flex-wrap: wrap;
			
			.product-item {
				width: 50%;
				padding: 10rpx;
				box-sizing: border-box;
				
				image {
					width: 100%;
					height: 300rpx;
					border-radius: 8rpx;
				}
				
				.product-info {
					padding: 10rpx;
					
					.title {
						font-size: 28rpx;
						color: #333;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						overflow: hidden;
					}
					
					.desc {
						font-size: 24rpx;
						color: #999;
						margin-top: 6rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						overflow: hidden;
					}
					
					.price-box {
						margin-top: 10rpx;
						
						.price {
							font-size: 32rpx;
							color: #ff0000;
							margin-right: 10rpx;
						}
						
						.original-price {
							font-size: 24rpx;
							color: #999;
							text-decoration: line-through;
						}
					}
				}
			}
		}
	}
</style>
